//  BPBottomBarTool.swift
//  BPWeiBo
//
//  Created by apple on 16/4/12.
//  Copyright © 2016年 apple. All rights reserved.

//在这里设置底部的工具栏,首先创建一个view,然后创建三个按钮,将三个按钮放进去,在将我们这三个按钮设置属性以及约束就好了

import UIKit
import SnapKit
class BPBottomBarTool: UIView {
    
    
    //定义相关的变量
    ///微博转发的数量
    private  var reposts_count:Int = 0
    ///微博评论的数量
    private  var comments_count:Int = 0
    ///微博点赞的数量
    private  var attitudes_count:Int = 0
    
    //在视图中加载模型色数据,将模型的数据赋值给我们view的控件
    var statusCellViewModel:BPStatusCellViewModel? {
        //当我们模型数据中的
        didSet{
            
            reposts_count = (statusCellViewModel?.reposts_count)!
            comments_count = (statusCellViewModel?.comments_count)!
            attitudes_count = (statusCellViewModel?.attitudes_count)!
            
            //设置btn的文字
            setText(transmitBtn, count: reposts_count, title: "转发")
            setText(commentBtn , count: comments_count, title: "评论")
            setText(zanBtn, count: attitudes_count, title: "赞")

        }
    }
    
    //MARK:处理微博数据的方法,当数据大于0,就不显示,当数据小于零就显示文字
    private func setText(btn: UIButton,count: Int,title: String) {
        if count > 0 {
            if count > 10000 {
                btn.setTitle(" \(count / 10000)万", forState: .Normal)
            }else {
                btn.setTitle(" \(count)", forState: .Normal)
            }
        }else {
            btn.setTitle(" \(title)", forState: .Normal)
        }
        
    }
    //MARK:声明三个按钮,不管在哪里赋值都会有值
    var transmitBtn:UIButton!
    var commentBtn:UIButton!
    var zanBtn:UIButton!
    private lazy var lineView:UIView = UIView()
    private lazy var lineView1:UIView = UIView()
    private lazy var lineView2:UIView = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setUpBtn()

        
        backgroundColor = UIColor.purpleColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setUpBtn() {
        
        lineView.backgroundColor = UIColor(white: 0.9, alpha:1)
        lineView1.backgroundColor = UIColor(white: 0.9, alpha:1)
        lineView2.backgroundColor = UIColor(white: 0.9, alpha:1)
        
        transmitBtn = UIButton(title: "转发", count: reposts_count, imgName: "timeline_icon_retweet")
        commentBtn = UIButton(title: "评论", count: comments_count, imgName: "timeline_icon_comment")
        zanBtn = UIButton(title: "赞", count: attitudes_count, imgName: "timeline_icon_like")
        
        addSubview(transmitBtn)
        addSubview(commentBtn)
        addSubview(zanBtn)
        addSubview(lineView)
        addSubview(lineView1)
        addSubview(lineView2)

        
        let btnWidth = ScreenWidth / 3
        let lineScale = UIScreen.mainScreen().scale
        let lineWidth = 1 / lineScale
        //设置黄建分割点
        let multiplied = 0.4
        
        //MARK:设置约束
        transmitBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self)
            make.height.equalTo(self.snp_height)
            make.width.equalTo(btnWidth)
        }
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(transmitBtn.snp_right)
            make.top.equalTo(self.snp_top)
            make.width.height.equalTo(transmitBtn)
        }
        zanBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(commentBtn.snp_right)
            make.top.equalTo(self.snp_top)
            make.width.height.equalTo(transmitBtn)
        }
        lineView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.left.equalTo(self.snp_left).offset(btnWidth)
            make.width.equalTo(lineWidth)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
        }
        lineView1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.right.equalTo(self.snp_left).offset(btnWidth*2)
            make.width.equalTo(lineWidth)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
        }
        lineView2.snp_makeConstraints { (make) in
            make.left.right.top.equalTo(self)
            make.height.equalTo(1)
        }
    
    }

}
